package edu.polsl.bio.str.manager.dao;

import edu.polsl.bio.str.common.entity.JobGroup;
import org.hibernate.LockMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * //todo class description
 * <p/>
 * Created on: Jun 14, 2011 4:37:33 PM <br/>
 * <a href="http://www.polsl.pl">www.polsl.pl</a>
 *
 * @author lukasz.olczak@polsl.pl
 */
public class JobGroupDaoImpl extends BaseDao implements JobGroupDao {
  /**
   * slf4j Logger.
   */
  private static final Logger log = LoggerFactory.getLogger(JobGroupDaoImpl.class.getName());

  @Override
  public JobGroup create(JobGroup jobGroup) {
    Serializable nodeId = getHibernateTemplate().save(jobGroup);
    return (JobGroup) getHibernateTemplate().get(JobGroup.class, nodeId);
  }

  @Override
  public JobGroup update(JobGroup jobGroup) {
    return (JobGroup) getHibernateTemplate().merge(jobGroup);
  }

  @Override
  public JobGroup getById(Long id) {
    return (JobGroup) getHibernateTemplate().get(JobGroup.class, id);
  }

  @Override
  public JobGroup getByIdForUpdate(Long id) {
    DetachedCriteria query = DetachedCriteria.forClass(JobGroup.class).add(Restrictions.eq("id", id))
        .setLockMode(LockMode.PESSIMISTIC_WRITE);

    return getUniqueResult(getHibernateTemplate().findByCriteria(query));
  }
}
